package com.zryc.shop.sms.controller;

import com.zryc.shop.base.Result;
import com.zryc.shop.sms.entity.FlashPromotion;
import com.zryc.shop.sms.service.IFlashPromotionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 限时购表 前端控制器
 * </p>
 *
 * @author zhaozhao
 * @since 2024-01-18
 */
@RestController
@Api(tags = "秒杀活动管理接口列表")
@RequestMapping("/sms/flash")
public class FlashPromotionController {


    @Autowired
    private IFlashPromotionService flashPromotionService;

    @GetMapping("/list")
    @ApiOperation("分页列表")
    public Result list(@RequestParam(required = false) @ApiParam(value = "品牌名称关键词") String keyword,
                       @RequestParam(defaultValue = "1") @ApiParam(value = "页码", required = true) Integer pageNum,
                       @RequestParam(defaultValue = "5") @ApiParam(value = "每页显示条数", required = true) Integer pageSize){
        return Result.ok(flashPromotionService.list(keyword,pageNum,pageSize));
    }

    @PostMapping("/create")
    @ApiOperation("添加秒杀活动")
    public Result create(@RequestBody FlashPromotion flashPromotion){
        flashPromotion.setCreateTime(LocalDateTime.now());
        return Result.ok(flashPromotionService.save(flashPromotion));
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除秒杀活动")
    public Result delete(@PathVariable @ApiParam(value = "活动ID", required = true) Long id){
        return Result.ok(flashPromotionService.removeById(id));
    }

    @PostMapping("/update/status/{id}")
    @ApiOperation("修改秒杀活动状态")
    public Result updateStatus(@PathVariable @ApiParam(value = "活动ID", required = true) Long id,
                               @RequestParam @ApiParam(value = "活动状态: 0-关闭 1-开启", required = true, allowableValues = "0,1") Integer status){
        return Result.ok(flashPromotionService.updateStatus(id,status));
    }

    @PostMapping("/update/{id}")
    @ApiOperation("更新秒杀活动")
    public Result updateStatus(@PathVariable @ApiParam(value = "活动ID", required = true) Long id, @RequestBody FlashPromotion flashPromotion){
        return Result.ok(flashPromotionService.updateFlash(id,flashPromotion));
    }

}
